<?php
//error_reporting(E_ALL);
//就近 www.jiujin.me
global $_W, $_GPC;
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
	ca('saoyue.list.view');
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
        if (!empty($_POST['realname'])) {
                $condition="";
                $_POST['realname'] = trim($_POST['realname']);
                $condition .= ' and ( m.weixin like :realname or m.realname like :realname or m.mobile like :realname or m.nickname like :realname)';
                $params[':realname'] = "%{$_POST['realname']}%";
                $list = pdo_fetchAll("SELECT r.*, FROM_UNIXTIME(r.createtime,'%Y-%m-%d') as cdate,FROM_UNIXTIME(r.createtime,'%T') as ctime,m.createtime,m.avatar,m.realname,m.mobile,m.weixin,m.company,m.gender,m.province,m.city,m.area,m.nickname  FROM " . tablename('mc_gifts_record') . " r left  join " . tablename('ewei_shop_member') . " m on r.uid=m.uid and r.uniacid=m.uniacid" . " WHERE r.uid =m.uid  AND r.remark='礼品充值' AND r.uniacid=" . $_W['uniacid'] . " $condition order by r.id desc",$params);
                foreach($list as $key => $value){
                    $list[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                }
        }else{
                $list = pdo_fetchAll("SELECT r.*, FROM_UNIXTIME(r.createtime,'%Y-%m-%d') as cdate,FROM_UNIXTIME(r.createtime,'%T') as ctime,m.createtime,m.avatar,m.realname,m.mobile,m.weixin,m.company,m.gender,m.province,m.city,m.area,m.nickname  FROM " . tablename('mc_gifts_record') . " r left  join " . tablename('ewei_shop_member') . " m on r.uid=m.uid and r.uniacid=m.uniacid" . " WHERE r.uid =m.uid  AND r.remark='礼品充值' AND r.uniacid=" . $_W['uniacid'] . " order by r.id desc LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
                foreach($list as $key => $value){
                    $list[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                }
                $total = pdo_fetchcolumn("SELECT count(*) FROM " . tablename('mc_gifts_record') . " WHERE remark='礼品充值' AND uniacid = '{$_W['uniacid']}' ORDER BY id DESC");
                $pager = pagination($total, $pindex, $psize);
        }
	if ($_GPC['export'] == 1) {
		foreach ($list as $key => $value){
                        if($value['gender']==1){
                        $list[$key]["gender"]="男";
                        }else{
                        $list[$key]["gender"]="女";  
                        }
                        $list[$key]["address"]=$value['province'].' '.$value['city'].''.$value['area']; 
		}
		
		$columns = array(
                    array('title' => 'ID', 'field' => 'id', 'width' => 12),
                    array('title' => '昵称', 'field' => 'nickname', 'width' => 12), 
                    array('title' => '微信号', 'field' => 'weixin', 'width' => 12),
                    array('title' => "充值时间", 'field' => 'createtime', 'width' => 24),
                    array('title' => '姓名', 'field' => 'realname', 'width' => 12),
                    array('title' => '手机号码', 'field' => 'mobile', 'width' => 12),
                    array('title' => '充值金额', 'field' => 'num', 'width' => 12),
                    array('title' => '性别', 'field' => 'gender', 'width' => 12),
                    array('title' => '所在城市', 'field' => 'address', 'width' => 12),
                    array('title' => '内容', 'field' => 'remark', 'width' => 12)
               );
                m('excel')->export($list, array("title" => "礼品充值记录导出" . date('Y-m-d-H-i', time()), "columns" => $columns));
	}
} elseif ($operation == 'pay_log') {
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
        if (!empty($_POST['realname'])) {
                $condition="";
                $_POST['realname'] = trim($_POST['realname']);
                $condition .= ' and ( m.weixin like :realname or m.realname like :realname or m.mobile like :realname or m.nickname like :realname)';
                $params[':realname'] = "%{$_POST['realname']}%";
                $list = pdo_fetchAll("SELECT r.*, FROM_UNIXTIME(r.createtime,'%Y-%m-%d') as cdate,FROM_UNIXTIME(r.createtime,'%T') as ctime,m.createtime,m.avatar,m.realname,m.mobile,m.weixin,m.company,m.gender,m.province,m.city,m.area,m.nickname  FROM " . tablename('mc_gifts_record') . " r left  join " . tablename('ewei_shop_member') . " m on r.uid=m.uid and r.uniacid=m.uniacid" . " WHERE r.uid =m.uid  AND r.remark<>'礼品充值' AND r.uniacid=" . $_W['uniacid'] . " $condition order by r.id desc",$params);
                foreach($list as $key => $value){
                    $list[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                }
        }else{
                $list = pdo_fetchAll("SELECT r.*, FROM_UNIXTIME(r.createtime,'%Y-%m-%d') as cdate,FROM_UNIXTIME(r.createtime,'%T') as ctime,m.createtime,m.avatar,m.realname,m.mobile,m.weixin,m.company,m.gender,m.province,m.city,m.area,m.nickname  FROM " . tablename('mc_gifts_record') . " r left  join " . tablename('ewei_shop_member') . " m on r.uid=m.uid and r.uniacid=m.uniacid" . " WHERE r.uid =m.uid  AND r.remark<>'礼品充值' AND r.uniacid=" . $_W['uniacid'] . " order by r.id desc LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
                foreach($list as $key => $value){
                    $list[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                }
                $total = pdo_fetchcolumn("SELECT count(*) FROM " . tablename('mc_gifts_record') . " WHERE remark<>'礼品充值' AND uniacid = '{$_W['uniacid']}' ORDER BY id DESC");
                $pager = pagination($total, $pindex, $psize);
        }
	if ($_GPC['export'] == 1) {
		foreach ($list as $key => $value){
                        if($value['gender']==1){
                        $list[$key]["gender"]="男";
                        }else{
                        $list[$key]["gender"]="女";  
                        }
                        $list[$key]["address"]=$value['province'].' '.$value['city'].''.$value['area']; 
		}
		
		$columns = array(
                    array('title' => 'ID', 'field' => 'id', 'width' => 12),
                    array('title' => '昵称', 'field' => 'nickname', 'width' => 12), 
                    array('title' => '微信号', 'field' => 'weixin', 'width' => 12),
                    array('title' => "充值时间", 'field' => 'createtime', 'width' => 24),
                    array('title' => '姓名', 'field' => 'realname', 'width' => 12),
                    array('title' => '手机号码', 'field' => 'mobile', 'width' => 12),
                    array('title' => '抵扣金额', 'field' => 'num', 'width' => 12),
                    array('title' => '性别', 'field' => 'gender', 'width' => 12),
                    array('title' => '所在城市', 'field' => 'address', 'width' => 12),
                    array('title' => '内容', 'field' => 'remark', 'width' =>50)
               );
                m('excel')->export($list, array("title" => "礼品抵扣记录导出" . date('Y-m-d-H-i', time()), "columns" => $columns));
	}
	
} elseif ($operation == 'gift_records') {
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
        if (!empty($_POST['realname'])) {
                $condition="";
                $_POST['realname'] = trim($_POST['realname']);
                $condition .= ' and ( m.realname like :realname or m.nickname like :realname)';
                $params[':realname'] = "%{$_POST['realname']}%";
                $list = pdo_fetchAll("SELECT r.*,m.avatar,m.realname,m.mobile,m.weixin,m.company,m.gender,m.province,m.city,m.area,m.nickname  FROM " . tablename('ewei_shop_commission_gift_log') . " r left  join " . tablename('ewei_shop_member') . " m on r.mid=m.id and r.uniacid=m.uniacid" . " WHERE r.mid =m.id  AND r.uniacid=" . $_W['uniacid'] . " $condition order by r.id desc",$params);
                foreach($list as $key => $value){
                    if($value['openid']){
                    $info=pdo_fetch('select * from '.tablename('ewei_shop_member').' where openid=:openid ',array(':openid'=>$value['openid']));
                    $list[$key]['nickname2']=$info['nickname'];
                    $list[$key]['realname2']=$info['realname'];
                    }
                    $list[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                    if($value['gettime']!=0){
                    $list[$key]['gettime'] = date('Y-m-d H:i:s',$value['gettime']);    
                    }else{
                    $list[$key]['gettime']="";   
                    }
                }
        }else{
                $list = pdo_fetchAll("SELECT r.*,m.avatar,m.realname,m.mobile,m.weixin,m.company,m.gender,m.province,m.city,m.area,m.nickname  FROM " . tablename('ewei_shop_commission_gift_log') . " r left  join " . tablename('ewei_shop_member') . " m on r.mid=m.id and r.uniacid=m.uniacid" . " WHERE r.mid =m.id  AND r.uniacid=" . $_W['uniacid'] . " order by r.id desc LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
                foreach($list as $key => $value){
                    $style=pdo_fetch('select * from '.tablename('ewei_shop_commission_gift_style').'where uniacid=:uniacid and id=:id',array(':uniacid'=>$_W['uniacid'],':id'=>$value['style']));
                    if($style){
                    $list[$key]['styletitle']=$style['title'];   
                    }
                    if($value['openid']){
                    $info=pdo_fetch('select * from '.tablename('ewei_shop_member').' where openid=:openid ',array(':openid'=>$value['openid']));
                    $list[$key]['nickname2']=$info['nickname'];
                    $list[$key]['realname2']=$info['realname'];
                    }
                    $list[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                    if($value['gettime']!=0){
                    $list[$key]['gettime'] = date('Y-m-d H:i:s',$value['gettime']);    
                    }else{
                    $list[$key]['gettime']="";   
                    }
                }
                $total = pdo_fetchcolumn("SELECT count(*) FROM " . tablename('ewei_shop_commission_gift_log') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id DESC");
                $pager = pagination($total, $pindex, $psize);
        }
        if(!empty($_GPC['refund'])){
            $log=pdo_fetch('select * from '.tablename('ewei_shop_commission_gift_log')."where uniacid = '{$_W['uniacid']}' and id='{$_GPC['refund']}' ");
            $user=pdo_fetch('select * from '.tablename('ewei_shop_member')." where uniacid = '{$_W['uniacid']}' and id='{$log['mid']}'");
            $mc_user=pdo_fetch('select * from '.tablename('mc_members')." where uniacid = '{$_W['uniacid']}' and uid='{$user['uid']}'");
            $gift=$mc_user['gift']+$log['money'];
            pdo_update('mc_members', array('gift'=>$gift),array('uid'=>$user['uid']));
            pdo_update('ewei_shop_commission_gift_log', array('status'=>2,'refundtime'=>time()),array('id'=>$_GPC['refund']));
            $msg = array(
                'first' => array('value' => " 礼品券退还通知！", "color" => "#4a5077"),
                'money' => array('title' => '退还金额', 'value' => '￥' . $log['money'] . '元', "color" => "#4a5077"),
                'time' => array('title' => '退还时间', 'value' =>date('Y-m-d H:i:s',time()), "color" => "#4a5077"),
                'remark' => array('value' => "\r\n请在“分销中心”中查收！谢谢您对小妖家的支持！", "color" => "#4a5077")
            );
            m('message')->sendCustomNotice($user['openid'], $msg);
            message('退还礼品成功！', $this->createWebUrl('gift/list', array('op' => 'gift_records')), 'success');
        }
	if ($_GPC['export'] == 1) {
		foreach ($list as $key => $value){
                        if($value['style']==1){$list[$key]['style']='元宵快乐';}
                        if($value['style']==2){$list[$key]['style']='中秋快乐';}
                        if($value['style']==3){$list[$key]['style']='常回家看看';}
                        if($value['style']==4){$list[$key]['style']='春节快乐';}
                        if($value['style']==5){$list[$key]['style']='端午快乐';}
                        if($value['style']==6){$list[$key]['style']='春天出游';}
                        if($value['style']==7){$list[$key]['style']='家庭幸福';}
                        if($value['style']==8){$list[$key]['style']='七夕快乐';}
                        if($value['status']==1){
                         $list[$key]['status']="已领取";   
                        }else{
                         $list[$key]['status']="未领取";   
                        }
		}
		
		$columns = array(
                    array('title' => 'ID', 'field' => 'id', 'width' => 12),
                    array('title' => '创建时间', 'field' => 'createtime', 'width' => 12), 
                    array('title' => '领取时间', 'field' => 'gettime', 'width' => 12),
                    array('title' => "创建人呢称", 'field' => 'nickname', 'width' => 24),
                    array('title' => '创建人姓名', 'field' => 'realname', 'width' => 12),
                    array('title' => '领取人呢称', 'field' => 'nickname2', 'width' => 12),
                    array('title' => '领取人姓名', 'field' => 'realname2', 'width' => 12),
                    array('title' => '金额', 'field' => 'money', 'width' => 12),
                    array('title' => '样式', 'field' => 'style', 'width' => 12),
                    array('title' => '是否领取', 'field' => 'status', 'width' =>50)
               );
                m('excel')->export($list, array("title" => "礼品赠送记录导出" . date('Y-m-d-H-i', time()), "columns" => $columns));
	}
}elseif($operation == 'style'){
    	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
        $list=pdo_fetchall('select * from '.tablename('ewei_shop_commission_gift_style')." where uniacid = '{$_W['uniacid']}' ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
        foreach($list as $key=>$value){
                $list[$key]['endtime']=date('Y-m-d',$value['endtime']);    
                $list[$key]['createtime']=date('Y-m-d',$value['createtime']);
        }
        $total = pdo_fetchcolumn("SELECT count(*) FROM " . tablename('ewei_shop_commission_gift_style') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id DESC");
        $pager = pagination($total, $pindex, $psize);
}elseif($operation == 'style_setup'){
   $id = intval($_GPC['id']);
   $info=pdo_fetch('select * from '.tablename('ewei_shop_commission_gift_style')." where uniacid = '{$_W['uniacid']}' and id=$id ");
   if($info){
   $info['endtime']=date('Y-m-d',$info['endtime']);
   }
   if($_POST['submit']==1){
   empty($_POST['title']) && message('请填写样式名称！');
   empty($_POST['color']) && message('请填写样式背景颜色！');
   empty($_POST['endtime']) && message('请填写有效期！');
   empty($_POST['sharetitle']) && message('请填写分享标题！');
   empty($_POST['sharedesc']) && message('请填写分享内容！');
   empty($_POST['image']) && message('请选择样式图片！');
   empty($_POST['enabled']) && message('请选择是否显示！');
   $_POST['endtime']=strtotime($_POST['endtime']);
   $data=array(
       'uniacid'=>$_W['uniacid'],
       'title'=>$_POST['title'],
       'color'=>$_POST['color'],
       'endtime'=>$_POST['endtime'],
       'sharetitle'=>$_POST['sharetitle'],
       'sharedesc'=>$_POST['sharedesc'],
       'image'=>$_POST['image'],
       'createtime'=>time(),
       'enabled'=>$_POST['enabled']
   );
   if(!empty($id)){  
      pdo_update('ewei_shop_commission_gift_style',$data,array('id'=>$id));  
   }else{
      pdo_insert('ewei_shop_commission_gift_style',$data);    
   }
   message('样式编辑成功！', $this->createWebUrl('gift/list', array('op' => 'style')), 'success');
   }
}elseif($operation == 'del'){
        $id = intval($_GPC['id']);
	if (empty($id)) {
		message('未找到指定的样式！');
	}
	$result = pdo_delete('ewei_shop_commission_gift_style',array('id' => $id));
	if (intval($result) == 1) {
		message('删除样式成功.', $this -> createWebUrl('gift/list',array('op' => 'style')), 'success');
	} else {
		message('删除样式失败.');
	}
}

load()->func('tpl');
include $this->template('web/gift/list');